//
//  LoginViewController.m
//  Weiweiyunshi
//
//  Created by 王译 on 17/2/13.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "LoginModel.h"
#import "MainViewController.h"
#import "AddressPhoneNumber.h"
@interface LoginViewController ()
{
    LoginView *view;
    NSString *url;
    
}
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    view = [[LoginView alloc]init];
    self.view = view;
    [self addtarget];
    self.view.userInteractionEnabled = YES;
    // Do any additional setup after loading the view.
}

-(void)createAlert{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户不存在或密码错误" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    [alert addAction:okButton];
    [self presentViewController:alert animated:YES completion:nil];
    
}

-(void)request{
    url = [NSString stringWithFormat:@"http://210.72.239.121:8088/SSIL/Login.do?userId=%@&pwd=%@",view.userNameTextField.text,view.passWordTextField.text];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"Code"]isEqualToString:@"1"]) {
            
            //获取userDefault单例
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            LoginModel *loginModel = [LoginModel yy_modelWithJSON:responseObject];
            //test
            
            [[LoginModel sharedInstance].userArr addObject:loginModel];
            [[LoginModel sharedInstance].userDic setObject:view.userNameTextField.text forKey:@"userId"];
            [[LoginModel sharedInstance].userDic setObject:view.passWordTextField.text forKey:@"passWord"];
            [userDefaults setObject:view.userNameTextField.text forKey:@"loginName"];
            [userDefaults setObject:view.passWordTextField.text forKey:@"loginPassword"];
            if (view.autoLoginBtn.selected == YES) {
                NSLog(@"自动登录");
                [userDefaults setObject:view.userNameTextField.text forKey:@"name"];
                [userDefaults setObject:view.passWordTextField.text forKey:@"password"];
                [userDefaults synchronize];
            }
            [self presentViewController:[MainViewController alloc] animated:YES completion:nil];
            //用户名或密码错误判断
        }else{
            NSLog(@"用户名或密码错误");
            [self createAlert];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    
}


-(void)addtarget{
    [view.loginBtn addTarget:self action:@selector(onLogin:) forControlEvents:UIControlEventTouchUpInside];
    [view.autoLoginBtn addTarget:self action:@selector(autoLogin:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)onLogin:(id)sender{
    [self request];
}



-(void)autoLogin:(UIButton *)button{
    if (button.selected == NO) {
        button.selected =YES;
        
    }else{
        button.selected = NO;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
